package Spring5.testdemo;

import Spring5.autowire.Emp;
import Spring5.bean.Orders;
import Spring5.collectiontype.Book;
import Spring5.collectiontype.Course;
import Spring5.collectiontype.Stu;
import Spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5Demo1 {

    //集合类型注入的参数
    @Test
    public void testCollection() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.test();
    }

    //集合类型注入的参数
    @Test
    public void testCollection2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        //在Spring里面，设置创建bean实例默认是单实例对象
        Book book1 = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);
        book1.test();
        //两者地址相同，设置配置文件中的scope值为prototype为多实例，两者地址便不相同
        System.out.println(book1);
        System.out.println(book2);
    }

    //工厂bean测试
    @Test
    public void testCollection3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Course course = context.getBean("myBean", Course.class);
        System.out.println(course);
    }

    //bean的生命周期测试
    @Test
    public void testBean4() {
        //ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);
        //手动让bean实例销毁
        context.close();
    }

    //自动装配测试
    @Test
    public void testAutowire() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
        Emp emp = context.getBean("emp", Emp.class);
        System.out.println(emp);
    }
}
